package com.example.list.bigbang;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.list.R;
import com.example.list.recyleview.MyAdapter;
import com.example.list.recyleview.TV_MSG;

import java.util.ArrayList;
import java.util.List;

public class BigBangActivity extends AppCompatActivity implements View.OnLongClickListener {

    private TextView tv_bang;
    private RecyclerView rl_bang;
    private List<TV_MSG> arrayList;
    private ArrayList<Integer> positionList = new ArrayList<Integer>();
    ;
    private List<String> strList;
    MyAdapterCharacter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_bang);
        tv_bang = (TextView) findViewById(R.id.bang_tv);
        rl_bang = (RecyclerView) findViewById(R.id.bang_rl);
        String poem =
                        "床前明月光，疑是地上霜，举头望明月，低头思故乡。" +
                        "离离原上草，一岁一枯荣，野火烧不尽，春风吹又生。" +
                        "床前明月光，疑是地上霜，举头望明月，低头思故乡。" +
                        "离离原上草，一岁一枯荣，野火烧不尽，春风吹又生。" +
                        "床前明月光，疑是地上霜，举头望明月，低头思故乡。" +
                        "离离原上草，一岁一枯荣，野火烧不尽，春风吹又生。" +
                        "床前明月光，疑是地上霜，举头望明月，低头思故乡。" +
                        "离离原上草，一岁一枯荣，野火烧不尽，春风吹又生。" +
                        "白日依山尽，黄河入海流，欲穷千里目，更上一层楼。";
        tv_bang.setText(poem);
        tv_bang.setOnLongClickListener(this);

        arrayList = new ArrayList<>();

        strList = new ArrayList<>();
        //单一布局
        adapter = new MyAdapterCharacter(this, arrayList);

        final GridLayoutManager layoutManager = new GridLayoutManager(this, 12);
        rl_bang.setLayoutManager(layoutManager);

        rl_bang.setAdapter(adapter);
        adapter.setOnItemClickListener(new MyAdapterCharacter.ItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {

                if (positionList.contains(position)) {
//                    在Java中为移除position 下标对应的元素，在当前条件下使用，会导致下标越界异常
//                    positionList.remove(position);


                    //强转后，移除值为position的元素（position本身），
                    // 这一点在kotlin是自动完成的。
                    positionList.remove(Integer.valueOf(position));

                } else {
                    positionList.add(position);
                }
                adapter.setPositinList(positionList);
            }
        });
    }

    @Override
    public boolean onLongClick(View view) {

        char[] bangcharArray = tv_bang.getText().toString().trim().toCharArray();
        for (int i = 0; i < bangcharArray.length; i++) {

            arrayList.add(new TV_MSG(String.valueOf(bangcharArray[i]), Color.GREEN));
        }
        adapter.notifyDataSetChanged();
        return true;
    }
}